package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: DickYang
 * Date: 2023-05-28
 * Time: 10:40
 */
public class AddOperation implements IOPeration {
    @Override
    public void work(BookList bookList) {
        Scanner scanner = new Scanner(System.in);
        Scanner scanner1 = new Scanner(System.in);
        System.out.println("请输入书名:");
        String name  = scanner.nextLine();
        System.out.println("请输入作者:");
        String author  = scanner.nextLine();
        System.out.println("请输入价格:");
        int price  = scanner.nextInt();
        System.out.println("请输入类型:");
        String type  = scanner1.nextLine();

        Book book = new Book(name,author,price,type);

        for (int i = 0; i < bookList.getUserSize(); i++) {
            if (!book.getName().equals(bookList.getBooks(i).getName())) {
                //记录当前书架的数量
                int cur = bookList.getUserSize();
                //把新书塞进书架
                bookList.setBooks(book);
                //书架的数量加1
                bookList.setUserSize(cur+1);
                System.out.println("新增图书成功!");
                return;
            }
        }
        System.out.println("图书已存在!");
    }
}
